﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AdmLaboratorios.Business;
using AdmLaboratorios.DTO;
using AdmLaboratorios;

namespace AdmLaboratorios
{
    public partial class frmPerfiles : Form
    {
        public frmPerfiles()
        {
            InitializeComponent();
        }

        void gv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView gv = (DataGridView)sender;
            int colIndex = e.ColumnIndex;
            int rowIndex = e.RowIndex;

            if (gv.Columns[colIndex] is DataGridViewButtonColumn)
            {
                if (gv.Columns[colIndex].Name == "cEdit")
                {

                }
                else if (gv.Columns[colIndex].Name == "cDelete")
                {
                    if (gv.Rows[rowIndex].DataBoundItem is PerfilDTO)
                    {
                        if (MessageBox.Show(
                            "¿Está seguro de que desea eliminar el perfil seleccionado?",
                            "Eliminar Perfil", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            Seguridad.DeletePerfil(((PerfilDTO)gv.Rows[rowIndex].DataBoundItem).Id);

                            RecargarPerfiles();
                        }
                    }
                }
            }
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            try
            {
                if (Validar())
                {
                    Seguridad.AltaPerfil(txNombre.Text.Trim());

                    RecargarPerfiles();
                }
            }
            catch (Exception ex)
            {
                FormsHelper.MsgErrorInesperado(ex);
            }
        }

        private bool Validar()
        {
            if (txNombre.Text.Trim() == string.Empty)
            {
                FormsHelper.MsgErrorValidacion();
                return false;
            }
            return true;
        }



        void gv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            DataGridView gv = (DataGridView)sender;
            FormsHelper.PintarBotones(e, gv, ilBotones);
        }

        private void cmbNiveles_SelectedIndexChanged(object sender, EventArgs e)
        {
            RecargarPerfiles();
        }

        private void frmPerfiles_Load(object sender, EventArgs e)
        {
            try
            {
                gv.CellPainting += new DataGridViewCellPaintingEventHandler(gv_CellPainting);
                gv.CellClick += new DataGridViewCellEventHandler(gv_CellClick);

                gv.AutoGenerateColumns = false;

                RecargarPerfiles();
            }
            catch (Exception ex)
            {
                FormsHelper.MsgErrorInesperado(ex);
            }
        }

        private void RecargarPerfiles()
        {
            gv.DataSource = null;
            gv.DataSource = Seguridad.Perfiles();
        }
    }
}
